package com.box.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.box.service.AlipayService;
import com.box.service.WeiXinService;
import com.box.utils.ResponseResult;
import com.box.utils.SessionUtils;
import com.google.gson.JsonArray;

@Controller
@RequestMapping("/alipay")
public class AlipayCtroller {
	
		@Autowired
		private AlipayService alipayService;
		
		@Autowired
		private WeiXinService weixinService;
	
		@RequestMapping(value="/pay",method={RequestMethod.GET})	
		public String AliPayForMoney(HttpServletRequest request,HttpServletResponse response,Model model) throws Exception{
	String[] parameterValues = request.getParameterValues("payid");
	for (String string : parameterValues) {//遍历获取的值
				Integer alipay=		Integer.parseInt(string);
if(alipay.equals(3)){//如果是3进到支付宝支付
	String money = alipayService.getMoney(request);
	
	model.addAttribute("result", money);
	return "pay/pay";
	
}else if(alipay.equals(4)){
	System.out.println(alipay);//如果是4进到微信支付
		
	List<String> k=new ArrayList<String>();
	 Map<String, Object> map = weixinService.LoadImage(request, response);//生成图片地址
		String imageUrl = 	map.get("imageUrl").toString();
		String money = map.get("total_fee").toString();
		String body = map.get("body").toString();
		String orderNum= map.get("out_trade_no").toString();
		String subject=map.get("subject").toString();
		k.add(imageUrl);
		k.add(money);
		k.add(orderNum);
		k.add(body);
		k.add(subject);

		model.addAttribute("k", k);
	


		return "WXpay/pay";


}
	}
			return null;
		}
	
		
		
		@RequestMapping(value="/notify_url",method={RequestMethod.GET,RequestMethod.POST})	
		public String notify_url(HttpServletRequest request,Model model) throws Exception{
			
			String goToNotify_urlPage = alipayService.GoToNotify_urlPage(request);
			model.addAttribute("goToNotify_urlPage", goToNotify_urlPage);
			
		return "pay/notify_url";
		}
		
		@RequestMapping(value="/return_url",method={RequestMethod.GET,RequestMethod.POST})	
		public String return_url(HttpServletRequest request,Model model) throws Exception{
			
			String return_urlPage = alipayService.return_url(request);
			model.addAttribute("return_urlPage", return_urlPage);
			
		return "pay/return_url";
		}
		
		
		@RequestMapping(value="/ajaxpay",method={RequestMethod.GET,RequestMethod.POST})		
		public  String AliPayForAjaxMoney(HttpServletRequest request,HttpServletResponse response,Model model) throws Exception{

 String[] parameterValues = request.getParameterValues("radio1");
 for (String string : parameterValues) {
	 Integer alipay=		Integer.parseInt(string);

				System.out.println(alipay+"............充值方式");
if(alipay.equals(3)){//如果是3进到支付宝支付
			String aliPayMoney = alipayService.getAliPayMoney(request);
	
model.addAttribute("result", aliPayMoney);
return "pay/pay";
	
}else if(alipay.equals(4)){
	System.out.println(alipay);//如果是4进到微信支付
		
	List<String> k=new ArrayList<String>();
	 Map<String, Object> map = weixinService.LoadImage(request, response);//生成图片地址
		String imageUrl = 	map.get("imageUrl").toString();
		String money = map.get("total_fee").toString();
		String body = map.get("body").toString();
		String orderNum= map.get("out_trade_no").toString();
		System.out.println(orderNum+"...............service中的订单号");
		String subject=map.get("subject").toString();
		k.add(imageUrl);
		k.add(money);
		k.add(orderNum);
		k.add(body);
		k.add(subject);

		model.addAttribute("k", k);
	


		return "WXpay/pay";


}
 }

			return null;
		}
	
		
		@RequestMapping(value="/gopay",method={RequestMethod.GET,RequestMethod.POST})
		public  String AliPayForAjaxGoPay(@RequestParam("result") String result,Model model,HttpServletResponse response) throws Exception{
			response.setHeader("Access-Control-Allow-Origin", "*");
			model.addAttribute("result", result);
			return "pay/pay";
		}
		
		@RequestMapping(value="/wxpay",method={RequestMethod.GET,RequestMethod.POST})
		public  String  AliPayForWaixinGoPay(HttpServletRequest request,HttpServletResponse response) throws Exception{
			this.wxpay(request, response);
			
			return "WXpay/pay";
		}
		
		protected void wxpay(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String[] parameterValues = request.getParameterValues("payid");
			for (String string : parameterValues) {//遍历获取的值
						Integer alipay=		Integer.parseInt(string);
						if(alipay.equals(4)){
							
				/*			//设置相应类型,告诉浏览器输出的内容为图片
						    response.setContentType("image/jpeg");
						    
						    //设置响应头信息，告诉浏览器不要缓存此内容
						    response.setHeader("pragma", "no-cache");
						    response.setHeader("Cache-Control", "no-cache");
						    response.setDateHeader("Expire", 0);*/
							weixinService.LoadImageTwo(request, response);
						}
			}
			
		}

	}
